<template>
    <div class="login_wrap">
        <!-- 登录表单区域 -->
        <div class="login_box">
            <!-- 表单logo -->
            <div class="login_logo">
                <img src="../assets/logo.png">
            </div>
            <!-- 表单区域 -->
            <div class="login_form">
                <!-- 输入区域 -->
                <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef" label-width="100px">
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="loginForm.username"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="loginForm.password"></el-input>
                    </el-form-item>
                </el-form>
                <!-- 按钮区域 -->
                <div class="login_btn">
                    <el-button @click="submitForm" type="primary">登录</el-button>
                    <el-button @click="resetForm" type="info">重置</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            // 登录表单参数
            loginForm: {
                username: '',
                password: ''
            },
            // 登录表单验证规则
            loginFormRules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        // 重置表单内容功能
        resetForm() {
            this.$refs.loginFormRef.resetFields();
        },
        // 登录功能
        submitForm() {
            // 验证表单数据格式
            this.$refs.loginFormRef.validate(async valid => {
                if (valid) {
                    const { data: res } = await this.$http.post('login', this.loginForm);
                    if (res.meta.status != 200) return this.$message.error('用户名或密码错误');
                    this.$message.success('登录成功');
                    window.sessionStorage.setItem('token', res.data.token);
                    this.$router.push('/home');
                } else {
                    this.$message.error('数据格式不正确');
                }
            });
        }
    }
}
</script>

<style lang="less" scoped>
.login_wrap {
    height: 100%;
    background-color: #254969;
}
.login_box {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 600px;
    height: 350px;
    border-radius: 8px;
    background-color: #fff;
    transform: translate(-50%, -50%);
}
.login_logo {
    position: absolute;
    left: 50%;
    top: -100px;
    padding: 10px;
    box-shadow: 0 0 10px #fff;
    border-radius: 50%;
    background-color: #fff;
    transform: translateX(-50%);
    img {
        border-radius: 50%;
    }
}
.login_form {
    position: absolute;
    top: 50%;
    width: 500px;
    transform: translateY(-50%);
}
.login_btn {
    position: absolute;
    right: 0;
}
</style>